//! # 窗口操作模块
//! 
//! 提供流处理中的窗口操作实现

use serde::{Deserialize, Serialize};
use std::time::Duration;

/// 窗口类型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WindowType {
    /// 滚动窗口
    Tumbling { size: Duration },
    /// 滑动窗口
    Sliding { size: Duration, advance: Duration },
    /// 会话窗口
    Session { gap: Duration },
    /// 计数窗口
    Count { count: usize },
}

/// 窗口配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowConfig {
    /// 窗口类型
    pub window_type: WindowType,
    /// 是否允许延迟数据
    pub allow_late_data: bool,
    /// 延迟数据容忍时间
    pub late_data_tolerance: Option<Duration>,
    /// 水印策略
    pub watermark_strategy: WatermarkStrategy,
}

/// 水印策略
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WatermarkStrategy {
    /// 事件时间
    EventTime,
    /// 处理时间
    ProcessingTime,
    /// 接收时间
    IngestionTime,
}

impl Default for WindowConfig {
    fn default() -> Self {
        Self {
            window_type: WindowType::Tumbling { size: Duration::from_secs(60) },
            allow_late_data: false,
            late_data_tolerance: None,
            watermark_strategy: WatermarkStrategy::ProcessingTime,
        }
    }
}

impl WindowConfig {
    /// 创建滚动窗口配置
    pub fn tumbling(size: Duration) -> Self {
        Self {
            window_type: WindowType::Tumbling { size },
            ..Default::default()
        }
    }
    
    /// 创建滑动窗口配置
    pub fn sliding(size: Duration, advance: Duration) -> Self {
        Self {
            window_type: WindowType::Sliding { size, advance },
            ..Default::default()
        }
    }
    
    /// 创建会话窗口配置
    pub fn session(gap: Duration) -> Self {
        Self {
            window_type: WindowType::Session { gap },
            ..Default::default()
        }
    }
    
    /// 创建计数窗口配置
    pub fn count(count: usize) -> Self {
        Self {
            window_type: WindowType::Count { count },
            ..Default::default()
        }
    }
}